package com.example.gsp.mybatis.util;

import java.lang.reflect.Field;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;

public final class ReflectionUtil {

    public static void setPropToBean(Object bean,String name,Object value){
        Field f = null;

        try {
            f = bean.getClass().getDeclaredField(name);
            f.setAccessible(true);
            f.set(bean,value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static <E> void setResultSetToList(E e, ResultSet resultSet) {
        Field[] fields = e.getClass().getDeclaredFields();
        Arrays.stream(fields).forEach(
                field -> {
                    try {
                        setPropToBean(e,field.getName(),resultSet.getObject(field.getName()));
                    } catch (SQLException e1) {
                        e1.printStackTrace();
                    }
                }
        );
    }
}
